package com.liyuanjinglyj.javacarddemo.adapter;

import com.liyuanjinglyj.javacarddemo.ResourceTable;
import com.liyuanjinglyj.javacarddemo.model.InfoItem;
import com.liyuanjinglyj.javacarddemo.utils.ImageNetWork;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import java.util.ArrayList;
import java.util.List;

public class InfoItemListProvider extends BaseItemProvider {
    private List<InfoItem> infoItemList=new ArrayList<>();
    private AbilitySlice abilitySlice;
    HiLogLabel label=new HiLogLabel(HiLog.LOG_APP, 0x00201, "TAG");
    public InfoItemListProvider(List<InfoItem> infoItemList,AbilitySlice abilitySlice) {
        this.infoItemList=infoItemList;
        this.abilitySlice=abilitySlice;
        HiLog.error(label,String.valueOf(infoItemList.size())+"11111");
    }

    @Override
    public int getCount() {
        return infoItemList == null ? 0 : infoItemList.size();
    }

    @Override
    public Object getItem(int i) {
        if (infoItemList != null && i >= 0 && i < infoItemList.size()){
            return infoItemList.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        if (component == null) {
            cpt = LayoutScatter.getInstance(this.abilitySlice).parse(ResourceTable.Layout_infoitem_listitem, null, false);
        } else {
            cpt = component;
        }
        InfoItem infoItem = this.infoItemList.get(i);
        HiLog.error(label,String.valueOf(i)+"11111");
        Text title=(Text)cpt.findComponentById(ResourceTable.Id_infoitem_listitem_title);
        title.setText(infoItem.getTitle());
        Image image=(Image)cpt.findComponentById(ResourceTable.Id_infoitem_listitem_image);
        new ImageNetWork(this.abilitySlice,image,infoItem.getThumb_url()).start();
        return cpt;
    }
}
